package com.jk.hearthstone.core;

import com.jk.hearthstone.card.biology.BaseBiology;
import com.jk.hearthstone.card.biology.hero.BaseHero;
import com.jk.hearthstone.data.Action;
import com.jk.hearthstone.enumeration.ActionType;
import com.jk.hearthstone.enumeration.SkillType;

import static com.jk.hearthstone.enumeration.ActionType.*;
import static com.jk.hearthstone.enumeration.SkillType.SKILL_TYPE_WITHOUTTARGET;
import static com.jk.hearthstone.enumeration.SkillType.SKILL_TYPE_WITHTARGET;

/**
 * @author jk
 * 动作的翻译和执行调用者
 */
public class Operator {

    public static boolean execute(Controller controller, Action action) {
        //如果是出牌
        if (action.actionType.equals(ACTION_TYPE_PLAY)) {
            return playExecute(controller,action);
        }
        //如果是攻击
        else if (action.actionType.equals(ACTION_TYPE_ATTACK)) {
            return attackExecute(controller,action);
        }
        //如果是技能
        else if (action.actionType.equals(ACTION_TYPE_HEROSKILL)) {
            return heroSkillExecute(controller,action);
        } else {
            return errorExecute();
        }
    }

    private static boolean playExecute(Controller controller, Action action) {
        if (action.target == null) {
            return controller.play(action.initiator);
        } else {
            return controller.play(action.initiator, action.target);
        }
    }

    private static boolean attackExecute(Controller controller, Action action) {
        controller.attack((BaseBiology) action.initiator, action.target);
        return true;
    }

    private static boolean heroSkillExecute(Controller controller, Action action) {
        //不需要选择目标的情况
        if (((BaseHero) action.initiator).skillType.equals(SKILL_TYPE_WITHOUTTARGET)) {
            controller.skill((BaseHero) action.initiator);
        }
        //需要选择目标  魔免会在生成行动时过滤
        else if (((BaseHero) action.initiator).skillType.equals(SKILL_TYPE_WITHTARGET)) {
            controller.skill((BaseHero) action.initiator, action.target);
        }
        return true;
    }

    private static boolean errorExecute() {
        return false;
    }
}
